package com.yzy.motion
{
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class Auto
	{
		protected var _edgeBehavior:String=BOUNCE;
		protected var _mass:Number=10.0;
		protected var _maxSpeed:Number=1;
		protected var _position:Vector2D;
		protected var _velocity:Vector2D;
		
		private var _maxForce:Number=20;
		private var _steeringForce:Vector2D;
		public var _wanderAngle:Number=0;
		private var _wanderDistance:Number=200;
		private var _wanderRadius:Number=5;
		private var _wanderRange:Number=1;
		//
		public static const WRAP:String="wrap";	
		public static const BOUNCE:String="bounce";
		//

		private var _rect:Rectangle;

		public function get maxForce():Number
		{
			return _maxForce;
		}

		public function set maxForce(value:Number):void
		{
			_maxForce = value;
		}

		public function get rect():Rectangle
		{
			return _rect;
		}

		public function set rect(value:Rectangle):void
		{
			_rect = value;
		}

		public function get maxSpeed():Number
		{
			return _maxSpeed;
		}

		public function set maxSpeed(value:Number):void
		{
			_maxSpeed = value;
		}

		public function get mass():Number
		{
			return _mass;
		}

		public function set mass(value:Number):void
		{
			_mass = value;
		}

		public function get edgeBehavior():String
		{
			return _edgeBehavior;
		}

		public function set edgeBehavior(value:String):void
		{
			_edgeBehavior = value;
		}

		public function get velocity():Vector2D
		{
			return _velocity;
		}

		public function set velocity(value:Vector2D):void
		{
			_velocity = value;
		}

		public function get position():Vector2D
		{
			return _position;
		}

		public function set position(value:Vector2D):void
		{
			_position = value;
			returnPoint.x=_position.x;
			returnPoint.y=_position.y;
		}

		
		
		
		/**
		 * 
		 */		
		public var returnPoint:Point=new Point();
		public var returnRotation:Number;
		
		
		public function Auto(){
			_steeringForce=new Vector2D();
		}
		
		public function updata():void{
			_steeringForce.truncate(_maxForce);
			_steeringForce=_steeringForce.divide(_mass);
			_velocity=_velocity.add(_steeringForce);
			_steeringForce=new Vector2D();
			
			
			_velocity.truncate(_maxSpeed);
			_position=_position.add(_velocity);
			if(_edgeBehavior==WRAP){
				wrap();
			}else if(_edgeBehavior==BOUNCE){
				bounce();
			}
			//
			returnPoint.x=position.x;
			returnPoint.y=position.y;
//			trace(returnPoint)
			//
			returnRotation=_velocity.angle*180/Math.PI;
		}
		
		public function wander():void{
			var center:Vector2D=velocity.clone().normalize().multiply(_wanderDistance);
			var offser:Vector2D=new Vector2D(0);
			offser.length=_wanderRadius;
			offser.angle=_wanderAngle;
			_wanderAngle+=Math.random()*_wanderRadius-_wanderRadius*.5;
			var force:Vector2D=center.add(offser);
			_steeringForce=_steeringForce.add(force)
		}
		
		private function wrap():void{
			if(position.x>_rect.right)position.x=_rect.left;
			if(position.x<_rect.left)position.x=_rect.right;
			if(position.y>_rect.bottom)position.y=_rect.top;
			if(position.y<_rect.top)position.y=_rect.bottom;	
		}
		private function bounce():void{
			if(position.x>_rect.right){
				position.x=_rect.right;
				velocity.x*=-1;
			}else if(position.x<_rect.left){
				position.x=_rect.left;
				velocity.x*=-1;
			}
			if(position.y>_rect.bottom){
				position.y=_rect.bottom;
				velocity.y*=-1;
			}else if(position.y<_rect.top){
				position.y=_rect.top;
				velocity.y*=-1;
			}
		}

		
	}
}